const matchBreadcrumbs = (routes, currentRoute) => {
  const breadcrumbs = [];

  const childrenRouteMatch = (routeList, targetPath) => {
    if (routeList && routeList.length > 0) {
      for (const item of routeList) {
        if (item.path && targetPath.includes(item.path)) {
          return true;
        }
      }
    }
    return false;
  };
  const buildBreadcrumbs = (routeList, targetPath) => {
    for (const route of routeList) {
      if (route.path &&
        targetPath.includes(route.path) &&
        !childrenRouteMatch(route.children, targetPath)
      ) {
        breadcrumbs.push({
          name: route.name,
          path: route.path,
          key: route.key,
          icon: route.icon
        });
        return true;
      }
      if (route.children && route.children.length > 0) {
        if (buildBreadcrumbs(route.children, targetPath)) {
          breadcrumbs.push({
            name: route.name,
            path: route.path,
            key: route.key,
            icon: route.icon
          });
          return true;
        }
      }
    }
    return false;
  }

  buildBreadcrumbs(routes, currentRoute);

  return breadcrumbs.reverse();
}

export default matchBreadcrumbs
